#include <iostream>
#include <list>

using std::cout;
using std::endl;
using std::list;

void test0()
{
    list<int> number = {1, 2, 5, 5, 6, 5, 5, 5, 5, 9};
    for(auto it = number.begin(); it != number.end(); ++it)
    {
        cout << *it << "  ";
    }
    cout << endl;

    for(auto it = number.begin(); it != number.end(); ++it)
    {
        if(5 == *it)
        {
            /* number.erase(it++); */
            it = number.erase(it);
        }
    }

    //删除完毕之后，再遍历
    for(auto it = number.begin(); it != number.end(); ++it)
    {
        cout << *it << "  ";
    }
    cout << endl;
}

void test()
{
    list<int> number = {1, 2, 5, 5, 6, 5, 5, 5, 5, 9};
    for(auto it = number.begin(); it != number.end(); ++it)
    {
        cout << *it << "  ";
    }
    cout << endl;

    //删除list中所有元素为5的值
    for(auto it = number.begin(); it != number.end();)
    {
        if(5 == *it)
        {
            it = number.erase(it);
            /* number.erase(it++); */
        }
        else
        {
            ++it;
        }
    }

    //删除完毕之后，再遍历
    for(auto it = number.begin(); it != number.end(); ++it)
    {
        cout << *it << "  ";
    }
    cout << endl;
}
int main(int argc, char *argv[])
{
    test();
    return 0;
}

